package com.tange.module.qrcode.scan;

import static android.content.Context.VIBRATOR_SERVICE;

import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;

import com.tange.base.toolkit.StatusNavUtils;
import com.tange.module.qrcode.biz.R;
import com.tg.appcommon.android.TGToast;


/**
 * 二维码扫描
 */
public class QrcodeScanFragment extends Fragment { //

    private QrCodeRecognitionView qrCodeView;

    private OnQrCodeScanListener onQrCodeScanListener;

    public void setOnQrCodeScanListener(OnQrCodeScanListener onQrCodeScanListener) {
        this.onQrCodeScanListener = onQrCodeScanListener;
    }

    public QrcodeScanFragment() {
    }

    public static QrcodeScanFragment newInstance() {
        QrcodeScanFragment fragment = new QrcodeScanFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        setToolbar();
        qrCodeView.onStart();
    }

    private void setToolbar() {
        QrcodeScanActivity addFragmentTabActivity = (QrcodeScanActivity) getActivity();
        if (addFragmentTabActivity != null) {
            addFragmentTabActivity.setToolbar(R.drawable.ic_back_btn_white, null);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbar();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.module_qrcode_fragment_qrcode_scan, container, false);

        qrCodeView   = view.findViewById(R.id.zxingview);
        qrCodeView.setOnQrCodeScanListener(new OnQrCodeScanListener() {
            @Override
            public void onSuccess(String qrCodeContent) {
                onScanQRCodeSuccess(qrCodeContent);
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onStart() {
        super.onStart();
        qrCodeView.onStart();
    }



    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onStop() {
        super.onStop();
        //qrCodeView.onStop();
        QrcodeScanActivity addFragmentTabActivity = (QrcodeScanActivity) getActivity();
        if (addFragmentTabActivity != null) {
            addFragmentTabActivity.getBackButton().setImageResource(R.mipmap.back_up);
            Window window = addFragmentTabActivity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            if (getContext() != null) {
                StatusNavUtils.setStatusBarColor(addFragmentTabActivity, getResources().getColor(R.color.device_add_background));
            }
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        qrCodeView.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) requireActivity().getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }



    public void onScanQRCodeSuccess(String result) {
        vibrate();
        if (!TextUtils.isEmpty(result) && result.length() != 12) {
            TGToast.showToast(R.string.incorrect_qrcode);
        }else {
            if (onQrCodeScanListener != null) {
                onQrCodeScanListener.onSuccess(result);
            }
        }
    }
}
